<template>
  <div class="inquiry-record">
    <div class="record-box">
      <div class="record-item" v-for="(item,index) in formData" :key="index">
        <p>{{item.text}}</p>
        <textarea v-model="item.checkText" :placeholder="'请输入'+item.text"></textarea>
      </div>
    </div>
    <div class="record-foot">
      <Button type="primary" icon="el-icon-finished" @click="onSave">保存</Button>
      <Button type="primary" icon="el-icon-refresh" @click="onRefresh">重置</Button>
    </div>
  </div>
</template>

<script>
import { Button, MessageBox, Message } from 'element-ui'
import { putDInquiry } from '@/api/api'
export default {
  components: {
    Button
  },
  props: ['inquiryid'],
  data() {
    return {
      formData: [
        { text: '主诉', checkText: '', filter: 'chiefComplaint' },
        { text: '病史', checkText: '', filter: 'medicalHistory' },
        { text: '体格检查', checkText: '', filter: 'physicalExamination' },
        {
          text: '辅助检查及治疗',
          checkText: '',
          filter: 'supplementaryExamination'
        },
        { text: '诊断', checkText: '', filter: 'diagnosis' }
      ]
    }
  },
  methods: {
    onSave() {
      MessageBox.confirm('确认保存?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {}
        this.formData.forEach(item => {
          data[item.filter] = item.checkText
        })
        console.log(data)
        // 保存诊断接口
        putDInquiry(this.inquiryid, data).then(res => {
          if (res.code === 200) {
            Message({
              type: 'success',
              message: '保存成功'
            });
          }
        })
      }).catch(() => {
        Message({
          type: 'info',
          message: '已取消保存'
        })
      });
    },
    onRefresh() {
      MessageBox.confirm('确认重置吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.formData = [
          { text: '主诉', checkText: '', filter: 'chiefComplaint' },
          { text: '病史', checkText: '', filter: 'medicalHistory' },
          { text: '体格检查', checkText: '', filter: 'physicalExamination' },
          {
            text: '辅助检查及治疗',
            checkText: '',
            filter: 'supplementaryExamination'
          },
          { text: '诊断', checkText: '', filter: 'diagnosis' }
        ]
        Message({
          type: 'success',
          message: '重置成功'
        });
      }).catch(() => {
        Message({
          type: 'info',
          message: '已取消重置'
        })
      });
    }
  }
}
</script>

<style lang="scss">
.record-box {
  padding: 0 20px;
  .record-item {
    display: flex;
    align-items: center;
    padding: 18px 0;
    border-bottom: 1px solid #eee;
    p {
      font-size: 18px;
      width: 18%;
    }
    textarea {
      flex: 1;
      height: 80px;
    }
  }
}
.record-foot {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
